
package com.showcai.controller.sms;

import com.showcai.controller.base.BaseController;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.redis.RedisCache;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.tools.RandomCode;
import com.showcai.tools.sms.SMSTools;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by lutao on 2017/7/14.
 */
@RequestMapping("/sms")
@Controller
public class SMSController extends BaseController {
    @Autowired
    RedisCache redisCache;

    @RequestMapping(value = "/snedSMS", method = RequestMethod.POST)
    @ResponseBody
    public String snedSMS(String phone) {
        logger.info("sendSMS===>>" + phone);
        ResultObj resultObj = new ResultObj();
        try {
            if(StringUtils.isBlank(phone)){
                throw new MyRunTimeExcption("手机号码不能为空！");

            }
            String code = RandomCode.getCode(6);
            redisCache.put(phone, code, 60);
            String smsParam = "{\"code\":\"" + code + "\"}";
            SMSTools.sendSMS(smsParam, phone, SMSTools.SMS_CODE);
            resultObj.setMsg("操作成功！");
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }
        return resultObj.toJson();
    }

}
